<?php

declare(strict_types=1);

namespace Dreamcat\ApiDocTest\TestCases;

use Dreamcat\ApiDoc\ApiDefineGeneratorInterface;
use Dreamcat\ApiDoc\ApiGenerators\ApiDocGenerator;
use Dreamcat\ApiDoc\Enums\DataType;
use Dreamcat\ApiDoc\Popo\RouterEntranceDefinition;
use Dreamcat\ApiDocTest\Demo\Ctl\EnumController;
use DreamCat\Array2Class\Utils\FeatureJudge;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

/**
 * 枚举测试
 * @author vijay
 */
class EnumTest extends TestCase
{
    /**
     * 测试带枚举控制器
     * @return void
     */
    public function testEnumController(): void
    {
        if (!FeatureJudge::hasEnum()) {
            self::assertEquals("", "");
            return;
        }
        $parser = $this->getParse();
        $def = $this->getRouteDef();
        $result = $parser->parseApi($def);
        self::assertEquals($def->getMethod(), $result->getMethod());
        self::assertEquals($def->getUri(), $result->getUri());
        self::assertEquals("test enum", $result->getName());
        self::assertEquals("", $result->getDesc());
        $body = $result->getBodyDefinition();
        self::assertEquals(DataType::OBJECT, $body->getDataType()->getValue());
        $list = $body->getObjectProperties();
        self::assertCount(3, $list);
        $checks = [
            "e1" => [
                "V1" => "testV1",
                "V2" => "testV2",
                "V3" => "V3",
            ],
            "e2" => [
                1 => "test1",
                2 => "test2",
                3 => "Int3",
            ],
            "e3" => [
                "v1" => "testStr1",
                "v2" => "testStr2",
                "v3" => "Str3",
            ],
        ];
        foreach ($list as $item) {
            self::assertEquals(DataType::ENUM, $item->getDataType()->getValue());
            self::assertArrayHasKey($item->getField(), $checks);
            self::assertEquals($checks[$item->getField()], $item->getEnums());
        }
    }

    /**
     * 获取解析器
     * @return ApiDefineGeneratorInterface
     */
    private function getParse(): ApiDefineGeneratorInterface
    {
        return new ApiDocGenerator();
    }

    private function getRouteDef(): RouterEntranceDefinition
    {
        $def = new RouterEntranceDefinition();
        $def->setAction(new ReflectionMethod(EnumController::class, "index"))
            ->setUri("/index")
            ->setMethod("POST");
        return $def;
    }
}
